package com.v1.video.domain;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.v1.video.AppContext;
import com.v1.video.activity.MainPageNewActivity;
import com.v1.video.option.db.PersonalCenterUploadDB;
import com.v1.video.util.Logger;

public class LoginInfo {
	private final String TAG = "LoginInfo";

	/** 服务器返回值 */
	private String code;
	/** 服务器返回消息 */
	private String msg;
	
	/** 用户唯一ID */
    private String userId = "";
    /** 用户老的id，由于4.4切用户系统导致出现，此userid只用在上传视频的地方 */
    private String userIdOld;
    /** 用户名 */
    private String userName;
    /** 昵称  */
    private String nickName;
    /** 登录名 */
    private String loginName;
    /** 登录密码 */
    private String loginPw;
    /** 头像url */
    private String userImg;
    /** 个人中心背景url */
    private String userBackImg;
    /** 个性签名 */
    private String detail;
    /** 性别 */
    private String sex;
    /** 坐标 */
    private String region;
    /** 状态 */
    private String state;
    /** 注册时间 */
    private String createTime;
    /** 上次登录时间 */
    private String lastloginTime;
    /** 视频数 */
    private String videos;
    /** 评论数 */
    private String comments;
    /** 分享数 */
    private String share;
    /** 编辑数 */
    private String editor;
    
    private String sessionId;
    /** 是否登录 */
    private boolean isLogin;
    /** 登录状态 */
    private int loginState = -1;
    /** 用户手机号 */
    private int userPhoneNum;
    /**访问人民明星所需带的字段*/
    private String tick = "";
    /** 用户手机号 */
    private boolean isSetUserName;
    private String nickname;
    private String isCertification;//V认证 1
    private String token = "";
    public String getFocusOnCounts() {
		return focusOnCounts;
	}

	public void setFocusOnCounts(String focusOnCounts) {
		this.focusOnCounts = focusOnCounts;
	}

	public String getFansCounts() {
		return fansCounts;
	}

	public void setFansCounts(String fansCounts) {
		this.fansCounts = fansCounts;
	}


	private String focusOnCounts;
    
    private String fansCounts;
    
    public static final int NOT_LOGIN = -1;
    public static final int LOGIN = 0;
    public static final int THIRDPARTY_LOGIN = 1;
    
//	public String getUserID() {
//		return userId;
//	}
//	
//	public void setUserID(String userID) {
//		this.userId = userID;
//	}
	
	public boolean isLogin() {
		return isLogin;
	}
	
	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}
	
	public String getTick() {
		return tick;
	}

	public void setTick(String tick) {
		this.tick = tick;
	}

	public int getLoginState() {
		return loginState;
	}
	
	public void setLoginState(int loginState) {
		this.loginState = loginState;
	}
	
	public int getUserPhoneNum() {
		return userPhoneNum;
	}
	
	public void setUserPhoneNum(int userPhoneNum) {
		this.userPhoneNum = userPhoneNum;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserImg() {
		return userImg;
	}

	public void setUserImg(String userImg) {
		this.userImg = userImg;
	}

	public String getDetail() {
		return detail;
	}
	
	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getUserBackImg() {
		return userBackImg;
	}

	public void setUserBackImg(String userBackImg) {
		this.userBackImg = userBackImg;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}


	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getLoginPw() {
		return loginPw;
	}

	public void setLoginPw(String loginPw) {
		this.loginPw = loginPw;
	}
	
	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getLastloginTime() {
		return lastloginTime;
	}

	public void setLastloginTime(String lastloginTime) {
		this.lastloginTime = lastloginTime;
	}

	public String getVideos() {
		return videos;
	}

	public void setVideos(String videos) {
		this.videos = videos;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public String getShare() {
		return share;
	}

	public void setShare(String share) {
		this.share = share;
	}

	public String getEditor() {
		return editor;
	}

	public void setEditor(String editor) {
		this.editor = editor;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}
	
    /**
     * @return the isSetUserName
     */
    public boolean isSetUserName() {
        return isSetUserName;
    }

    /**
     * @param isSetUserName the isSetUserName to set
     */
    public void setSetUserName(boolean isSetUserName) {
        this.isSetUserName = isSetUserName;
    }


    private static LoginInfo instance;
	
	private LoginInfo(){
		instance = this;
	}
	
	public static LoginInfo getInstance(){
		if(instance == null){
			new LoginInfo();
			instance.getLoginInfo(AppContext.getInstance());
		}
		return instance;
	}
	
	public static void setInstance(LoginInfo loginInfo){
		LoginInfo.instance = loginInfo;
	}
	
	public void clear(Context context){
		
		setUserId("");
		setToken("");
		setUserName("");
		setUserImg("");
		setNickName("");
		setUserBackImg("");
		setDetail("");
		setSex("");
		setTick("");
		setRegion("");
		setState("");
		setUserIdOld("");
		setCreateTime("");
		setLastloginTime("");
		setVideos("");
		setComments("");
		setShare("");
		setEditor("");
		setLogin(false);		
		setLoginState(-1);
		setSetUserName(false);
		setBangdinglist(null);
		setIsuser(null);
		setIsCertification("");
		saveInstance(context);
		AppContext.setHeadBitmap(null);
		PersonalCenterUploadDB.getInstance(context).db_close();
	}
	
	/**
	 * 设置用户登录状态
	 * 
	 * @param userId
	 * @param userName
	 * @param userImg
	 * @param detail
	 * @param sex
	 * @param region
	 * @param focusOnCounts
	 * @param fansCounts
	 * @param releaseCounts
	 * @param forwardCounts
	 * @param collectionCounts
	 * @param isLogin
	 * @param loginState
	 */
	public void saveInstance(Context context){
		Logger.i(TAG, "saveInstance_本方法执行了,nickName="+nickName);
		SharedPreferences settings = context.getSharedPreferences("loginInfo", 0);
		SharedPreferences.Editor spEditor = settings.edit();
		spEditor.putString("userId", userId);
		spEditor.putString("userName", userName);
		spEditor.putString("loginName", loginName);
		spEditor.putString("loginPw", loginPw);
		spEditor.putString("userImg", userImg);
		spEditor.putString("detail", detail);
		spEditor.putString("sex", sex);
		spEditor.putString("region", region);
		spEditor.putString("state", state);
		spEditor.putString("createTime", createTime);
		spEditor.putString("lastloginTime", lastloginTime);
		spEditor.putString("videos", videos);
		spEditor.putString("comments", comments);
		spEditor.putString("share", share);
		spEditor.putString("editor", editor);
		spEditor.putBoolean("isLogin", isLogin);
		spEditor.putInt("loginState", loginState);
		spEditor.putBoolean("isSetUserName", isSetUserName);
		spEditor.putString("nickName", nickName);
		spEditor.putString("userBackImg", userBackImg);
		spEditor.putString("isCertification", isCertification);
		spEditor.putString("token", token);
		spEditor.putString("useridOld", userIdOld);
		spEditor.putString("tick", tick);
		if(isuser != null){
			spEditor.putString("mobileno", isuser.getMobileno());
			spEditor.putBoolean("iseditusername", isuser.isIseditusername());
		}
		if(Bangdinglist != null && Bangdinglist.size() > 0){
			spEditor.putInt("bindListSize", Bangdinglist.size());
			for(int i=0; i<Bangdinglist.size(); i++){
				BindInfo bindInfo = Bangdinglist.get(i);
				if(bindInfo != null){
					if(!TextUtils.isEmpty(bindInfo.getInfoString())){
						spEditor.putString("bindInfo" + i, bindInfo.getInfoString());
				    }
				}
			}			
		}
		
		spEditor.commit();
	}
	
	public void getLoginInfo(Context context){
		
		SharedPreferences settings = context.getSharedPreferences("loginInfo", 0);
		
		if(!settings.contains("userId")){
			return;
		}
		setUserId(settings.getString("userId", ""));
		setUserName(settings.getString("userName", ""));
		setLoginName(settings.getString("loginName", ""));
		setLoginPw(settings.getString("loginPw", ""));
		setUserImg(settings.getString("userImg", ""));
		setDetail(settings.getString("detail", ""));
		setSex(settings.getString("sex", ""));
		setRegion(settings.getString("region", ""));
		setState(settings.getString("state", ""));
		setCreateTime(settings.getString("createTime", ""));
		setLastloginTime(settings.getString("lastloginTime", ""));
		setVideos(settings.getString("videos", ""));
		setComments(settings.getString("comments", ""));
		setShare(settings.getString("share", ""));
		setEditor(settings.getString("editor", ""));
		setLogin(settings.getBoolean("isLogin", false));
		setLoginState(settings.getInt("loginState", -1));
		setSetUserName(settings.getBoolean("isSetUserName", false));
		setNickName(settings.getString("nickName", ""));
		setUserBackImg(settings.getString("userBackImg", ""));
		setIsCertification(settings.getString("isCertification", "0"));
		setToken(settings.getString("token", ""));
		setUserIdOld(settings.getString("useridOld", ""));
		setTick(settings.getString("tick", ""));
		if(settings.contains("mobileno")){
			IsuserInfo isuserInfo = new IsuserInfo();
			isuserInfo.setMobileno(settings.getString("mobileno", ""));
			isuserInfo.setIseditusername(settings.getBoolean("iseditusername", false));
			setIsuser(isuserInfo);
		}
		if(settings.contains("bindListSize")){
			
			int bindListSize = settings.getInt("bindListSize", -1);
			if(Bangdinglist == null){
//				Logger.i(TAG, "getLoginInfo方法_Bangdinglist == null");		
				Bangdinglist = new ArrayList<BindInfo>();
			}
			else{
//				Logger.i(TAG, "getLoginInfo_Bangdinglist.clear()");
				Bangdinglist.clear();
			}
			
			for(int i=0; i<bindListSize; i++){			
				String bindStr = settings.getString("bindInfo" + i, "");
				Gson gson = new Gson();
				Bangdinglist.add(gson.fromJson(bindStr, BindInfo.class));
			}
			
		}
		
		
	}
	
    private List<BindInfo> Bangdinglist = new ArrayList<BindInfo>();
	private IsuserInfo isuser;
	private ResultInfo result;
	
	public List<BindInfo> getBangdinglist() {
        return Bangdinglist;
    }

    public void setBangdinglist(List<BindInfo> bangdinglist) {
    	
        Bangdinglist = bangdinglist;
    }

    public IsuserInfo getIsuser() {
        return isuser;
    }

    public void setIsuser(IsuserInfo isuser) {
        this.isuser = isuser;
    }
    public ResultInfo getResult() {
        return result;
    }

    public void setResult(ResultInfo result) {
        this.result = result;
    }
    
    public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getIsCertification() {
		return isCertification;
	}

	public void setIsCertification(String isCertification) {
		this.isCertification = isCertification;
	}

	public String getUserIdOld() {
		return userIdOld;
	}

	public void setUserIdOld(String userIdOld) {
		this.userIdOld = userIdOld;
	}
}

